package com.lfz.quality_effect.controller;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet;
import com.lfz.quality_effect.model.Test;
import com.lfz.quality_effect.model.entity.TIndexItem;
import com.lfz.quality_effect.model.entity.TIndexScore;
import com.lfz.quality_effect.service.TIndexItemService;
import com.lfz.quality_effect.service.TIndexScoreService;
import com.lfz.quality_effect.utils.ExcelListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
public class TestController {

    @Autowired
    private TIndexItemService indexItemService;
    @Autowired
    private TIndexScoreService indexScoreService;
    @GetMapping(value = "/test")
    public void test(){
        ExcelListener listener = new ExcelListener();
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("C:\\Users\\LFZ-开发1\\Downloads\\瑞昌市人民法院指标得分明细20230302.xls");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //传入参数
        ExcelReader excelReader = new ExcelReader(inputStream, null, listener);
        //读取信息
        excelReader.read(new Sheet(1, 1, Test.class));

        List<Object> objectList = listener.getDatas();

        List<TIndexItem> testList = new ArrayList<>();

        List<TIndexScore> list = new ArrayList<>();

        for (int i=0;i<objectList.size();i++){
            Test mediateCaseDTO = (Test)objectList.get(i);
            //testList.add(mediateCaseDTO);
//            TIndexItem indexItem = new TIndexItem();
//            indexItem.setIndexId(mediateCaseDTO.getIndexId());
//            indexItem.setIndexName(mediateCaseDTO.getIndexName());
//            indexItem.setModule(mediateCaseDTO.getModule());
//            indexItem.setIndexType(mediateCaseDTO.getIndexType());
//            indexItem.setIsOnline(mediateCaseDTO.getIsOnline());
//            indexItem.setIndexScore(mediateCaseDTO.getIndexScore());
//            indexItem.setCreateTime(LocalDateTime.now());
//            testList.add(indexItem);
            TIndexScore indexScore = new TIndexScore();
            indexScore.setIndexScore(mediateCaseDTO.getIndexScore());
            indexScore.setCreateTime(LocalDateTime.now());
            indexScore.setIndexValue(mediateCaseDTO.getIndexValue());
            indexScore.setIndexAddScore(mediateCaseDTO.getIndexAddScore());
            indexScore.setDataTime("2023-3-4");
            indexScore.setIndexId(mediateCaseDTO.getIndexId());
            list.add(indexScore);
        }
       // indexItemService.saveBatch(testList);
        indexScoreService.saveBatch(list);
    }
}
